package com.gs.common.exception;

import com.gs.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandler {

    @ResponseBody
    @org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
    public R authorizationException(Exception e) {

        log.error("全局异常处理器......", e);
        e.printStackTrace();
        if(e instanceof AuthorizationException) {
            return R.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
        return R.error(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
